package org.jeecg.modules.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import liquibase.pro.packaged.D;
import liquibase.pro.packaged.L;
import org.jeecg.modules.demo.base.entity.ZnzfApprovalAppartAdmin;
import org.jeecg.modules.demo.base.entity.ZnzfApprovalOrRejectParam;
import org.jeecg.modules.demo.base.entity.ZnzfEmployee;
import org.jeecg.modules.demo.base.entity.ZnzfEmployeeScoreItem;
import org.jeecg.modules.demo.base.mapper.ZnzfApprovalAppartAdminMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeScoreItemMapper;
import org.jeecg.modules.demo.base.service.IZnzfApprovalAppartAdminService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 部门审核专员设定
 * @Author: jeecg-boot
 * @Date:   2022-03-27
 * @Version: V1.0
 */
@Service
public class ZnzfApprovalAppartAdminServiceImpl extends ServiceImpl<ZnzfApprovalAppartAdminMapper, ZnzfApprovalAppartAdmin> implements IZnzfApprovalAppartAdminService {

    @Resource
    private ZnzfApprovalAppartAdminMapper znzfApprovalAppartAdminMapper;

    @Resource
    private ZnzfEmployeeScoreItemMapper znzfEmployeeScoreItemMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysDepartMapper sysDepartMapper;

    @Resource
    private ZnzfEmployeeMapper znzfEmployeeMapper;

    /**
     * 根据userId查找具备审核资格的审核人员
     * @param userId
     * @return
     */
    @Override
    public ZnzfApprovalAppartAdmin getAdminByUserId(String userId) {
        LambdaQueryWrapper<ZnzfApprovalAppartAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfApprovalAppartAdmin::getUserId, userId);
        ZnzfApprovalAppartAdmin admin = znzfApprovalAppartAdminMapper.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 查找所有的审核人员
     * @return
     */
    @Override
    public List<ZnzfApprovalAppartAdmin> queryAllApprovalDepartAdmin() {
        LambdaQueryWrapper<ZnzfApprovalAppartAdmin> queryWrapper = new LambdaQueryWrapper<>();
        List<ZnzfApprovalAppartAdmin> arrayList = new ArrayList<>();
        List<ZnzfApprovalAppartAdmin> list = znzfApprovalAppartAdminMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i ++) {
            ZnzfApprovalAppartAdmin znzfApprovalAppartAdmin = list.get(i);
            SysUser sysUser = sysUserMapper.selectById(znzfApprovalAppartAdmin.getUserId());
            SysDepart depart = null;
            if (sysUser != null) {
                znzfApprovalAppartAdmin.setUserName(sysUser.getRealname());
                depart = sysDepartMapper.selectById(sysUser.getDepartIds());
            }
            if (depart != null) {
                znzfApprovalAppartAdmin.setDepartName(depart.getDepartName());
            }
            arrayList.add(znzfApprovalAppartAdmin);
        }
        return arrayList;
    }

    /**
     * 查找所有职员资质
     * @return
     */
    @Override
    public List<ZnzfEmployeeScoreItem> queryAllEmployeeScoreItems() {
        LambdaQueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = new LambdaQueryWrapper<>();
        List<ZnzfEmployeeScoreItem> itemList = znzfEmployeeScoreItemMapper.selectList(queryWrapper);
        return itemList;
    }

    /**
     * 根据scoreItemId查着所有审核信息
     * @param scoreItemId
     * @return
     */
    @Override
    public List<ZnzfEmployeeScoreItem> queryEmployeeScoreItemsByItemId(String scoreItemId) {
        LambdaQueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployeeScoreItem::getScoreItemId, scoreItemId);
        List<ZnzfEmployeeScoreItem> itemList = znzfEmployeeScoreItemMapper.selectList(queryWrapper);
        return itemList;
    }


    /**
     * 根据esiId查找对应的职员审核资质项
     * @param znzfApprovalOrRejectParam
     * @param realName
     * @return
     */
    @Override
    public int setState(ZnzfApprovalOrRejectParam znzfApprovalOrRejectParam, String realName) {
        String esiId = znzfApprovalOrRejectParam.getEsiId();
        String state = znzfApprovalOrRejectParam.getState();
        String note = znzfApprovalOrRejectParam.getNote();
        String opinion = znzfApprovalOrRejectParam.getOpinion();
        ZnzfEmployeeScoreItem znzfEmployeeScoreItem = znzfEmployeeScoreItemMapper.selectById(esiId);
        if (znzfEmployeeScoreItem != null) {
            if (Integer.parseInt(state) == 2 || Integer.parseInt(state) == 0) {

                znzfEmployeeScoreItem.setState(state);
                znzfEmployeeScoreItem.setApprovalPeople(realName);
                znzfEmployeeScoreItem.setApprovalTime(new Date());
                if (note != null) znzfEmployeeScoreItem.setNote(note);
                if (opinion != null) znzfEmployeeScoreItem.setOpinion(opinion);

                LambdaUpdateWrapper<ZnzfEmployeeScoreItem> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(ZnzfEmployeeScoreItem::getId, esiId);
                znzfEmployeeScoreItemMapper.update(znzfEmployeeScoreItem, updateWrapper);
                if (Integer.parseInt(state) == 2) return 2;
                else if (Integer.parseInt(state) == 0) {
                    String uid = znzfEmployeeScoreItem.getUserId();
                    LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(ZnzfEmployee::getUserId, uid);
                    ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
                    if (znzfEmployee == null)
                        return 0;
                    //审核通过一项后，更新职工表的最新得分
                    znzfEmployee.setLatestScore(znzfEmployee.getLatestScore() + znzfEmployeeScoreItem.getScore());
                    int i = znzfEmployeeMapper.updateById(znzfEmployee);
                    if (i == 0) {
                        System.out.println("更新分数失败");
                        return 0;
                    }
                    System.out.println("更新分数成功");
                    return 1;
                }
            } else {
                // state不是0， 2，不能更新状态
                return 0;
            }
        }
        return 0;
    }
}
